#ifndef FORM_YS_BZGL_H
#define FORM_YS_BZGL_H

#include "ys_bzgl_3103.h"
#include "pub_pop_get_item.h"
#include "pub_pop_delay.h"
#include "pub_pop_get_value.h"
#include "pub_sqthread.h"
#include "ys_cap_setup.h"

#include <QWidget>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkInterface>
#include <QNetworkRequest>
#include <QJsonObject>
#include <QJsonArray>
#include <QSqlQuery>
#include <QMap>
#include <QSqlQueryModel>
#include <QStandardItemModel>
#include <QComboBox>
#include <QTimer>
#include <QCheckBox>
#include <QMessageBox>
#include <QPlainTextEdit>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QCameraInfo>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>

namespace Ui {
class Form_ys_bzgl;
}

class Form_ys_bzgl : public QWidget
{
    Q_OBJECT

public:
    explicit Form_ys_bzgl(QWidget *parent = nullptr);
    ~Form_ys_bzgl();

    void closeEvent(QCloseEvent *event);

signals:
    void destroyW( QString );
    void trigTimer();
    void trig_senior();

private slots:

    bool eventFilter(QObject *obj, QEvent *event);

    QString get_cur_drug_left(QString, QString);//get current drug count in patient person

    QString cur_sm3(QString mode, QString plan = "");   //mode : screen屏幕内容, database数据库内容

    int save_rec_hist(QString fld_name, //字段名
                       QString fld_new, //新值
                       QString fld_old, //旧值
                       QString tmstmp, //时间戳
                       QString clinic_code);//
    bool med_rec();//整理病志
    bool push_5465(QString);//推送诊间支付消息
    void save_5465(QString mode);//保存诊间支付消息, mode = 1,保存；mode = 0,撤销；
    void process_out_grade_apply();//检查超职级用药申请
    void save_senior_recipe_no();
    void display_aux_check();
    void save_medical_plan();
    void pic_fitToView();
    void pic_zoomIn();
    void pic_zoomOut();

    bool remote_file_syncronize(QString, QString);
    void cameraImageCaptured(int, QImage image);

    void change_camera(QString);
    void camera_initial();
    void restore_diag_comb(QString);//恢复诊断下拉列表
    void diag_new_model(QPlainTextEdit * , QString, QString );//新增加诊断，模版处理方式选择
    void diag_new_model_le(QLineEdit * , QString, QString );//新增加诊断，模版处理方式选择
    void process_combine_item();

    QMap<QString, QString> config_diag(QMap<QString, QString>); //多次保存病志，处理多诊断，清理无效诊断

    bool busi_2261();

    bool diag_complete(QComboBox *med_diag, QComboBox *tr_diag,QComboBox *tr_symp);

    void save_recipe_error(QString, QString);

    void diag1_state_enable();
    void diag2_state_enable();
    void diag2_state_disable();
    void diag3_state_disable();

    void cbb_tr_diag_text_changed(
            QString diag_text,
            QComboBox *cur,
            QComboBox *next,
            QComboBox *third,
            QComboBox *cur_symp,
            QComboBox *next_symp,
            QComboBox *relate_cbb,
            QLineEdit *dest_le_name,
            bool is_first);

    void cbb_tr_symp_text_changed(
            QString diag_text,
            QComboBox *cur,
            QComboBox *next,
            QComboBox *third,
            QComboBox *cur_symp,
            QComboBox *next_symp,
            QComboBox *relate_cbb,
            QLineEdit *dest_le_name,
            bool is_first);

    void cbb_med_text_changed(
            QString diag_text,
            QComboBox * cur,
            QComboBox * next,
            QComboBox * third,
            QLineEdit * dest_le,
            QComboBox * relate_tr_diag,
            QComboBox * relate_tr_symp,
            QComboBox * next_tr_diag,
            QComboBox * next_tr_symp,
            bool is_first);

    void getDiags();
    void getRegDateRecipe();
    void tmTvWidthReset();
    void tmDiagTimeout();
    void tmDiagTimerStarter();
    bool saveBZ(int);//保存病志基础信息 ，参数 0 按钮触发， 参数 1 保存处方时调用
    bool drugCheck(); //用药合理性校验
    bool undrug_check(); //非药品项目合理性校验
    bool statusCheck(QString); //环境合理性校验 0病志， 1处方
    void fillGroup();   //填充组套列表
    void tvRecipeNewRefresh(int mode);  //刷新recipe列表, mode 1/0, all clear or input clear
    void threadMsg(QString s);
    QString getDictValue(QString dic_type, QString search_value); //字典类别，检索值或代码
    QString getDeptName(QString);   //由科室代码检索科室名称
    QString getIcpcCode(QString);
    //QString getNatCode(QString);
    QString getNatCode(QString,QString);
    void fresh_e_rec_zd();  //撤销处方时，清理 ys_bz_e_rec 中的诊断记录

    void fillCombo(QComboBox *cbb,
                   QString sq ,
                   int ind,
                   bool clear_before_fill = true,
                   QMap<QString, QString> * mp = NULL);

    bool call_5960(QJsonObject &inpu);
    bool call_2261();

    void localHistory(QString bzh);

    void on_pbExit_clicked();

    void on_pbFresh_clicked();

    void on_tvList_clicked(const QModelIndex &index);

    void on_pbHistory_clicked();

    void on_rbIn_clicked();

    void on_rbOut_clicked();

    void on_cbbGms_currentTextChanged(const QString &arg1);

    void on_cbbJws_currentTextChanged(const QString &arg1);

    void on_cbbBzmb_currentIndexChanged(const QString &arg1);

    void on_pbModelDelete_clicked();

    void on_pbModeSave_clicked();

    void on_pbSave_clicked();

    void on_pbClear_clicked();

    void on_pbCheck3101_clicked();

    void on_tvList_doubleClicked(const QModelIndex &index);

    void on_tvHistory_clicked(const QModelIndex &index);

    void on_leHzzs_returnPressed();

    void on_cbbReason_activated(const QString &arg1);

    void on_cbbBzmb_activated(const QString &arg1);

    void on_leJws_returnPressed();

    void on_leGms_returnPressed();

    void on_tvHistory_doubleClicked(const QModelIndex &index);

    void on_pbItemClear_clicked();

    void on_tabWidget_currentChanged(int index);

    void on_cbName_clicked();

    void on_cbCardNo_clicked();

    void on_cbIdenno_clicked();

    void on_pbModeSave_2_clicked();

    void on_pbModelDelete_2_clicked();

    void on_cbbJchy_currentIndexChanged(const QString &arg1);

    void on_pbSaveBZ_clicked();

    void on_pbSaveCK_clicked();

    void on_tvGroup_clicked(const QModelIndex &index);

    void on_pbRemoveItem_clicked();

    void on_pbAddItem_clicked();

    void on_leSearchG_returnPressed();

    void on_pbSearch_clicked();

    void on_tvRecipeNew_clicked(const QModelIndex &index);

    void on_tvRecipeNew_doubleClicked(const QModelIndex &index);

    void on_pbRecipeClear_clicked();

    void on_pbRemoveUncharge_clicked();

    void on_tvRecipe_clicked(const QModelIndex &index);

    void on_tvRecipe_doubleClicked(const QModelIndex &index);

    void on_leItemName_textChanged(const QString &arg1);

    void on_pbGetLastInfo_clicked();

    void on_pbEditAgain_clicked();

    void on_tvGroup_doubleClicked(const QModelIndex &index);

    void on_ckbDisplayCancel_clicked();

    void on_rb_mode_med_clicked();

    void on_le_diag1_textChanged(const QString &arg1);

    void on_le_diag2_textChanged(const QString &arg1);

    void on_le_diag3_textChanged(const QString &arg1);

    void on_tv_aux_check_clicked(const QModelIndex &index);

    void on_tv_aux_check_doubleClicked(const QModelIndex &index);

    void on_pb_remove_clicked();

    void on_cbbRange_currentTextChanged(const QString &arg1);

    void on_cbbDiag1_currentTextChanged(const QString &arg1);

    void on_cbbDiag2_currentTextChanged(const QString &arg1);

    void on_cbbDiag3_currentTextChanged(const QString &arg1);

    void on_cbb_tr_diag1_currentTextChanged(const QString &arg1);

    void on_cbb_tr_diag2_currentTextChanged(const QString &arg1);

    void on_cbb_tr_diag3_currentTextChanged(const QString &arg1);

    void on_cbb_tr_symptom1_currentTextChanged(const QString &arg1);

    void on_cbb_tr_symptom2_currentTextChanged(const QString &arg1);

    void on_cbb_tr_symptom3_currentTextChanged(const QString &arg1);

    void on_pb_clear_base_clicked();

    void on_pb_2261_clicked();

    void on_pb_save_doct_ord_clicked();

    void on_ckb_refine_recorder_clicked();

    void on_pb_get_history_clicked();

    void on_pb_alter_diag_clicked();

    void on_tb_exchange_diag0_clicked();

    void on_pb_fresh_diag_clicked();

    void on_tb_exchange_diag1_clicked();

    void on_cbb_yz_currentTextChanged(const QString &arg1);

    void on_pb_cap_take_clicked();

    void on_pb_cap_setup_clicked();

    void on_cbb_cam_id_currentTextChanged(const QString &arg1);

    void on_pb_cap_remove_clicked();

    void on_pb_pic_save_clicked();

    void on_pb_pic_refresh_clicked();

    void on_tv_pic_display_doubleClicked(const QModelIndex &index);

    void on_tv_pic_display_clicked(const QModelIndex &index);

    void on_pb_pic_upload_clicked();

    void on_pb_pic_describe_change_clicked();

    void on_pb_5465_clicked();

    void on_pb_drug_remain_clicked();

    void on_pb_drug_remain_1_clicked();

private:
    Ui::Form_ys_bzgl *ui;

    QSqlQuery *qrm, *qrmt, *qro, *qrot;
    //QSqlQueryModel mdList ;//

    bool initialFinished;//界面初始化完成标志
    bool remoteQuery;   //远程查询标志。
    bool busi_continue; //交易状态
    QString sq;
    QString cur_item_code;  //当前正在编辑的项目代码
    QString ybysdm; //医保医师代码

    QSqlQueryModel *md, *mdHist, *mdRecipe, *mdRecipeNew, *mdGroupPop, *mdSearchPop;
    QSqlQueryModel *md_check; //辅诊
    QSqlQueryModel *md_pics;//图像

    QStandardItemModel *mdRelated, *mdGroup;

    QNetworkRequest request, request_zjzf;
    QNetworkAccessManager manager ;
    QMap<QString,QString> mpPatientInfo;
    QMap<QString,QString> mpRecipeItemInfo; //手工录入处方，存储中间值
    QString zd1c,zd2c,zd3c,jzyyc;   //诊断代码，就诊原因代码

    Ys_cap_setup * cap_setup;
    Ys_bzgl_3103 * pop_3103;
    Pub_pop_delay * dlgDelay;
    Pub_pop_get_value * dlgGetValue;
    Pub_pop_get_item * dlgGetItem;
    sqThread * sqlThread;
    QStringList sqlMsg;

    int newRecipeRecItemCount;

    QTimer *tmDiag;
    QTimer * tmTvWidth;
    QString strDiag;
    QStringList diags;

    QMessageBox my_msg;
    QPushButton *bt_overwriter, *bt_append, *bt_ignore;

    QString last_hospital_info; //最后一次住院信息

    bool diag_model, simple_model; //模版使用选择
    QMap<QString, QString> doct_ord;

    QCamera * camera;
    QCameraViewfinder * viewfinder;
    QCameraImageCapture* imageCapture;

    bool camera_initialized;
    bool has_camera;

    int pic_num;

    QString pic_path;

    QMap<QString, QString> pic_describe;

    QString remote_path;

    QGraphicsView * view;
    QGraphicsScene * scene;
    QGraphicsPixmapItem *pixmapItem;

    double pic_scale_factor;
    bool reload_med_rec;    //当前正在调取历史记录
    QStringList recipe_list_5465;//准备保存到诊间支付信息中的处方号
};

#endif // Form_ys_bzgl_H
